local M = {
	dragRect = nil,
}

local resolver = require("components.resolver")

local function addDragEvent(target, cfg)
	local function onMid(event)
		if not M.dragRect then
			M.dragRect = display.newRect(0, 0, 100, 20)
			M.dragRect.alpha = 0.3
		end

		M.dragRect.x = event.x
		M.dragRect.y = event.y
	end

	local function onCancelMid(event)
		if M.dragRect then
			M.dragRect:removeSelf()
			M.dragRect = nil
		end
		eventDispatcher:dispatchEvent("compRelease", { cfg = cfg, x = event.x, y = event.y})
	end

	inputUtils.mouseDelegate({
		target = target,
		onMid = onMid,
		onCancelMid = onCancelMid,
	})
end

function M.createItem(params)
	local gItem = display.newGroup()

	local obj = resolver.resolverCfg(params.cfg)

	gItem:insert(obj)

	addDragEvent(obj, params.cfg)

	return gItem
end

return M